package com.medusa.aps.business.modules.modeling.mp.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.medusa.aps.business.model.BaseEntity;
import com.medusa.aps.business.model.enums.ProductionType;
import com.medusa.aps.business.modules.modeling.model.output.BomItemVo;
import com.medusa.aps.business.modules.modeling.model.output.BomTranslateViewVO;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;

import static com.medusa.aps.business.model.constant.DigitConstants.MYSQL_DECIMAL_10_2;
import static com.medusa.aps.business.model.constant.DigitConstants.ZERO_TWELVE_DIGIT_NUMBER_PROMPTS;


/**
 * BOM项实体类
 *
 * @author yuxh
 * @since 2023-08-14 17:37:43
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class BomItem extends BaseEntity<BomItem> {
    /**
     * 主键
     */
    private Long id;
    /**
     * 层级
     */
    @TableField(value = "`level`")
    private Integer level;
    /**
     * bom id
     */
    @Schema(description = "bom id")
    private Integer bomId;
    /**
     * bom编码
     */
    private String bomCode;
    /**
     * 物料id
     */
    private Long materialId;
    /**
     * 物料编码
     */
    private String materialCode;
    /**
     * 物料名称
     */
    private String materialName;
    /**
     * 上层子项BomId
     */
    private Long parentId;
    /**
     * 单位
     */
    private String unit;
    /**
     * 单件用量
     */
    @NotNull
    @Digits(integer = 10, fraction = 2, message = "单件用量最多保留两位小数")
    @DecimalMin(value = "0.01", message = "单件用量最小为1")
    @DecimalMax(value = MYSQL_DECIMAL_10_2, message = "单件用量" + ZERO_TWELVE_DIGIT_NUMBER_PROMPTS)
    private BigDecimal unitUsage;
    /**
     * 物料规格
     */
    @Schema(description = "物料规格")
    private String materialSpecification;
    /**
     * 生产类型
     */
    @Schema(description = "生产类型")
    private ProductionType productionType;
    /**
     * 备注
     */
    private String remarks;

    /**
     * 获取主键值
     *
     * @return 主键值
     */
    @Override
    public Serializable pkVal() {
        return this.id;
    }

    public BomTranslateViewVO toBomTranslateViewVO() {
        return new BomTranslateViewVO()
                .setId(id)
                .setLevel(level)
                .setMaterialSpecification(materialSpecification)
                .setMaterialCode(materialCode)
                .setMaterialName(materialName)
                .setParentId(parentId)
                .setUnitUsage(unitUsage);
    }

    public BomItemVo toBomItemVo(String parentMaterialCode, String parentMaterialName) {
        return new BomItemVo()
                .setId(id)
                .setLevel(level)
                .setMaterialId(materialId)
                .setMaterialCode(materialCode)
                .setMaterialName(materialName)
                .setUnit(unit)
                .setUnitUsage(unitUsage)
                .setMaterialSpecification(materialSpecification)
                .setProductionType(productionType)
                .setParentId(parentId)
                .setRemarks(remarks)
                .setParentMaterialCode(parentMaterialCode)
                .setParentMaterialName(parentMaterialName);
    }
}

